from Common.Utils import blog_driver
from Common.FormHelper import FormHelper
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from Register import Register

class Login:
    url=""
    driver=""
    register_instance=""
    creds=""
    def __init__(self):
        self.url="http://49.235.61.184:8080/user/login"
        self.driver=blog_driver.driver
        self.driver.get(self.url)
        self.driver.implicitly_wait(10)
        self.form = FormHelper(self.driver)
        self.register_instance=Register()
        self.register_instance.RegisterSucTest()# 只注册一次
        self.driver.get(self.url) # 注册后跳转回登录页

    def clear(self):
        self.form.clear_fields(["#username", "#password"])

    def commit(self,username,password):
        self.form.fill_fields({
            "#username": username,
            "#password": password,
        })

    def _verify_error_message(self, css_selector, expected_text):
        self.form.verify_error_message(css_selector, expected_text, timeout=10)

    #登录成功
    def LoginSuTest(self):
        #注册一个成功的账号
        self.clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(self.register_instance.username)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(self.register_instance.password)
        blog_driver.GetScreeShot()
        self.form.click("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")

        #证明登录成功
        WebDriverWait(self.driver,10).until(EC.url_contains("/home"))
        self._verify_error_message("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > nav > ol > li:nth-child(2) > span.ant-breadcrumb-link > span","首页")
        # self.driver.get(self.url)
    #登录失败
    def LoginFailTest(self, username="", password="", error_rules=None):
        self.driver.get(self.url)
        # 清空
        self.clear()
        # 填充
        self.commit(username, password)
        # 提交
        self.form.click("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")
        blog_driver.GetScreeShot()

        # 验证所有错误提示
        if error_rules:
            for css_selector, expected_text in error_rules.items():
                self._verify_error_message(css_selector, expected_text)

        blog_driver.GetScreeShot()
        self.driver.get(self.url)

    #登录失败--都为空
    def FailTest1(self):
        self.LoginFailTest("","",
                           error_rules={
                               "#username_help > div":"用户名是必填项！",
                               "#password_help > div":"密码是必填项！"
                           })

    #登录失败--账号为空
    def FailTest2(self):
        self.LoginFailTest("",self.register_instance.password,
                           error_rules={
                               "#username_help > div": "用户名是必填项！"
                           })

        # 登录失败--密码为空
    def FailTest3(self):
        self.LoginFailTest(self.register_instance.username,"",
                               error_rules={
                                   "#password_help > div":"密码是必填项！"
                               })


# login=Login()
# # login.LoginSuTest()
# login.LoginSuTest()
# login.FailTest1()
# login.FailTest2()
# login.FailTest3()







